<?php
// Contains commonly used functions. Based on Lab 8 demo.
session_start();

if (!isset($_SESSION['user_id']))
  $_SESSION['user_id'] = "";

// User type selection variables
$choose_one_select = htmlspecialchars("");
$choose_one_select1 = htmlspecialchars("");
$choose_one_select2 = htmlspecialchars("");
$a_selection = htmlspecialchars("");
$b_selection = htmlspecialchars("");
$s_selection = htmlspecialchars("");
$u_selection = htmlspecialchars("");
$g_selection = htmlspecialchars("");
$yp_selection = htmlspecialchars("");
$np_selection = htmlspecialchars("");

//********************************************************************
// Run the SQL, and return the error flag and the cursor in an array.
// The array index "flag" contains the flag.
// The array index "cursor" contains the cursor.
//********************************************************************
function execute_sql_in_oracle($sql) {

  $connection = oci_connect("gq012","brupch");

  if(!$connection) {

    // Failed to connect
    display_oracle_error_message(null);
    die("Failed to connect.");
  }
  
  $cursor = oci_parse($connection, $sql);

  if (!$cursor) {

    display_oracle_error_message($connection);
    oci_close($connection);

    // SQL failed 
    die("SQL parsing failed.");
  }

  // Execute SQL query
  $result = oci_execute($cursor);

  // Close the connection with oracle
  oci_close($connection);  

  $return_array["flag"] = $result;
  $return_array["cursor"] = $cursor;

  return $return_array;
}

//*****************************************************************
// Using existing execute_sql_in_oracle() function and modified it
// to read in SQL queries from an array and execute them.
//*****************************************************************
function execute_sqls_in_oracle($sqlArray) {

  $connection = oci_connect("gq012", "brupch");

  if(!$connection) {
    // Failed to connect
    display_oracle_error_message(null);
    die("Failed to connect.");
  }

  // Loop through SQL queries array and execute them
  for ($i = 0; $i < sizeof($sqlArray); $i++) {

    $cursor = oci_parse($connection, $sqlArray[$i]);

    if (!$cursor) {

      display_oracle_error_message($connection);
      oci_close($connection);

      // SQL failed 
      die("SQL parsing failed.");
    }
  }

  $result = oci_execute($cursor);

  // Close the connection with oracle
  oci_close($connection);  

  $return_array["flag"] = $result;
  $return_array["cursor"] = $cursor;

  return $return_array;
}

//**************************************************************
// Verify user page access privilege
//**************************************************************
function verify_privileges($page_type) {
  if ((empty($_SESSION['aflag']) or $_SESSION['aflag'] == "0") and $page_type == "A") {
    // Unauthorized access
    header("Location:error_page.php");
  }
  if ((empty($_SESSION['sflag']) or $_SESSION['sflag'] == "0") and $page_type == "S") {
    // Unauthorized access
    header("Location:error_page.php");
  }
} 

//**************************************************************
// Use to reset add new user form fields
//**************************************************************
function resetForm() {
  $_SESSION['client_id'] = "";
  $_SESSION['password'] = "";
  $_SESSION['fname'] = "";
  $_SESSION['lname'] = "";
  $_SESSION['dob'] = "";
  $_SESSION['client_type'] = "";
  $_SESSION['student_type'] = "";
  $_SESSION['input_pflag'] = "";
  $_SESSION['street'] = "";
  $_SESSION['city'] = "";
  $_SESSION['state'] = "";
  $_SESSION['zip'] = "";
   $_SESSION['input_grade'] = "";
  $_SESSION['input_cno'] = "";
  $_SESSION['input_seqid'] = "";
}

//***************************************************
// Use to format the address display on 
// User Management Main using the appropriate 
// database fields
//***************************************************
function &formatAddress($street, $city, $state, $zip) {
  $address = "";
  if (!empty($street) and !empty($city) and !empty($state))
    $address = $street . "<br>".$city . ", ".$state . " ".$zip;
  else if (!empty($street) and !empty($city)) 
    $address = $street . "<br>".$city;
  else if (!empty($street) and empty($city))
    $address = $street;
  else
    $address = "<br>".$state . " ".$zip;
  return $address;
}

//***************************************************
// Takes an executed errored oracle cursor as input.
// Display an interpreted error message.
//***************************************************
function display_oracle_error_message($resource) {
  if (is_null($resource))
    $err = oci_error();
  else
    $err = oci_error($resource);

  echo "<BR />";
  echo "Oracle Error Code: " . $err['code'] . "<BR />";
  echo "Oracle Error Message: " . $err['message'] . "<BR />" . "<BR />";
  
  if ($err['code'] == 1)
    echo("Duplicate Values.  <BR /><BR />");
  else if ($err['code'] == 984 or $err['code'] == 1861 
    or $err['code'] == 1830 or $err['code'] == 1839 or $err['code'] == 1847
    or $err['code'] == 1858 or $err['code'] == 1841)
    echo("Wrong type of value entered.  <BR /><BR />");
  else if ($err['code'] == 1400 or $err['code'] == 1407)
    echo("Required field not correctly filled.  <BR /><BR />");
  else if ($err['code'] == 2292)
    echo("Child records exist.  Need to delete or update them first.  <BR /><BR />");
}
?>